package com.example.administrator.gpsstationbletool.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.example.administrator.gpsstationbletool.R;

import java.util.ArrayList;
import java.util.List;

public class CmdAdapter extends BaseAdapter {

    public static class commandItem {
        public String name;
        public String value;
        int id;

        public commandItem(String _name,String _value) {
            this.name = _name;
            this.value = _value;
            this.id = 0;
        }
    }
    private Context context;
    private List<commandItem> commandList;

    public CmdAdapter(Context context) {
        this.context = context;
        commandList = new ArrayList<>();
    }

    public int addCmd(commandItem cmd) {
        commandList.add(cmd);
        return commandList.indexOf(cmd);
    }

    public void removeCmd(int pos) {
                commandList.remove(pos);
    }

    public void updateCmd(int pos,commandItem item)
    {
        commandList.remove(pos);
        commandList.add(pos,item);

    }

    public List<commandItem> getCommandList()
    {
        return  commandList;
    }

    public void clear() {
        commandList.clear();
    }

    @Override
    public int getCount() {
        return commandList.size();
    }

    @Override
    public commandItem getItem(int position) {
        if (position > commandList.size())
            return null;
        return commandList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.adapter_param, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            holder.img_blue = (ImageView) convertView.findViewById(R.id.img_blue);
            holder.txt_name = (TextView) convertView.findViewById(R.id.txt_name);
            holder.txt_value = (TextView) convertView.findViewById(R.id.text_value);
            holder.btn_send = (Button) convertView.findViewById(R.id.btn_sendCmd);
            holder.btn_delete = (Button) convertView.findViewById(R.id.btn_delete);
        }

        final commandItem cmditem = getItem(position);
        final int index = position;
        if (cmditem != null) {
            holder.txt_name.setText(cmditem.name);
            holder.txt_value.setText(cmditem.value);

        }

        holder.btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onSendCmd(cmditem);
                }
            }
        });

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onDelete(index);
                }
            }
        });

        holder.img_blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onItemEdit(index);
                }
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView img_blue;
        TextView txt_name;
        TextView txt_value;
        Button btn_send;
        Button btn_delete;
    }

    public interface OnDeviceClickListener {
        void onSendCmd(commandItem cmd);

        void onDelete(int index);

        void onItemEdit(int index);
    }

    private OnDeviceClickListener mListener;

    public void setOnDeviceClickListener(OnDeviceClickListener listener) {
        this.mListener = listener;
    }

}
